//var game = new Phaser.Game(1200, 400, Phaser.AUTO, 'runner', { preload: preload, create: create, update: update });


Main.game = function (game) {
	this.game = game;
};

Main.game.prototype = {
	
	player:Phaser.Sprite,
	shadow:Phaser.Sprite,
	shadow2:Phaser.Sprite,
    medidor:Phaser.Sprite,

	platforms: Phaser.Group,
	cursors:null,
	fps : 25,
	bombs:Phaser.Group,
	diamonds:Phaser.Group,
	score : 0,
	scoreText:Phaser.Text,
	posText:Phaser.Text,
	direction_right : true,
	s:null,
	s2:null,
	velocity:-152,
	velocity_background:-4,
	scale:1,
	distancia:75,
	transicion_abajo:false,
	transicion_arriba:false,
	carretera_abajo:true,
	modo_salto:false,
	count_salto:false,
	health:5,
	healthText:Phaser.Text,
	
	preload:function () {
		
	},
	
	create:function () {
	
		//  A simple background for our game
		this.game.add.sprite(0, 0, 'sky');
		
		//Attempt to create a parallax background
		this.s2 = this.game.add.tileSprite(0, this.game.world.height - 400, 1200, 400, 'fondo');
		this.s = this.game.add.tileSprite(0, this.game.world.height - 400, 1200, 400, 'fondo2');
		
		//Medidor
		this.game.add.sprite(15, 15, 'medidor');

		//some diamonds to collect & bombs to avoid
		this.diamonds = this.game.add.group();
		this.bombs = this.game.add.group();
				
		this.shadow = this.game.add.sprite(0, 305, 'dude');
		this.shadow.animations.add('right', [1, 2,3,4,5,6]);
		this.shadow.animations.add('jump', [7]);
		this.shadow.alpha=0.4;
		this.shadow.body.collideWorldBounds = true;

		this.shadow2 = this.game.add.sprite(74, 305, 'dude');
		this.shadow2.animations.add('right', [1, 2,3,4,5,6]);
		this.shadow2.animations.add('jump', [7]);
		this.shadow2.alpha=0.2;
		this.shadow2.body.collideWorldBounds = true;

		// The player and its settings
		this.player = this.game.add.sprite(149, 305, 'dude');

		//  Player physics properties. Give the little guy a slight bounce.
		//player.body.bounce.y = 0.2;
		//player.body.gravity.y = 10;
		this.player.body.collideWorldBounds = true;
		this.player.body.setSize(50,75,25,25);


		//  Our two animations, walking left and right.
		//player.animations.add('right', [0, 1, 2, 3, 4 ,5, 6, 7,8,9,10,11,12,13,14,15,16,17]);
		this.player.animations.add('right', [1, 2,3,4,5,6]);
		this.player.animations.add('jump', [7]);

		//  The score
		this.scoreText = this.game.add.text(170, 120, 'Score: 0', { fontSize: '32px', fill: '#fff' });
		this.healthText = this.game.add.text(170, 170, 'Health: 5', { fontSize: '32px', fill: '#fff' });

		this.posText  = this.game.add.text(800, 120, 'Pos: ', { fontSize: '32px', fill: '#fff' });;

		//  Our controls.
		this.cursors = this.game.input.keyboard.createCursorKeys();
		this.jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
		this.pauseButton = this.game.input.keyboard.addKey(Phaser.Keyboard.P);

	},
	update:function () {
		//Debug Pos variables
		this.posText.content = 'PosX: ' + this.player.body.x + ' PosY: ' + this.player.body.y + ' Scale: ' + this.scale;

		//Parallax effect
		this.s.tilePosition.x += this.velocity_background;
		this.s2.tilePosition.x += this.velocity_background/2;
		
		if(this.carretera_abajo){
			this.shadow.body.x=this.player.body.x-this.distancia;
			this.shadow.body.y=this.player.body.y-25;
			
			this.shadow2.body.x=this.player.body.x-this.distancia*1.75;
			this.shadow2.body.y=this.player.body.y-25;
		}
		else{
			this.shadow.body.x=this.player.body.x-this.distancia;
			this.shadow.body.y=this.player.body.y-20;
			
			this.shadow2.body.x=this.player.body.x-this.distancia*1.75;
			this.shadow2.body.y=this.player.body.y-20;
		}
		
		this.shadow.scale.setTo(this.scale, this.scale);
		this.shadow2.scale.setTo(this.scale, this.scale);

		this.player.animations.play('right',this.fps,true);
		this.shadow.animations.play('right',this.fps,true);
		this.shadow2.animations.play('right',this.fps,true);
		
		this.bombs.setAll('speed', this.velocity);
		this.diamonds.setAll('speed', this.velocity);

		//ledge.body.velocity.x=-6;
		//  Collide the player and the stars with the platforms
		this.game.physics.collide(this.player, this.platforms);
		//game.physics.collide(shadow, platforms);
		//game.physics.collide(stars, platforms);
		//game.physics.collide(shadow, platforms);
		//game.physics.collide(shadow2, platforms);

		//  Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
		this.game.physics.overlap(this.player, this.diamonds, this.collectDiamond, null, this);
		this.game.physics.overlap(this.player, this.bombs, this.touchBomb, null, this);

		//  Reset the players velocity (movement)
		this.player.body.velocity.x = 0;

		//Scale the player
		this.player.scale.setTo(this.scale, this.scale);

		//Bombas y diamantes aleatorias
		var aleat = Math.floor((Math.random()*100)+1);
		if(aleat==1) {
			//Randomize diamond
			var diamond = this.diamonds.create(1200, 350,'diamond');
			diamond.body.velocity.x = this.velocity;
		}
		else if(aleat==2) {
			//Randomize diamond
			var diamond = this.diamonds.create(1200, 270,'diamond');
			diamond.scale.setTo(0.75, 0.75);
			diamond.body.velocity.x = this.velocity;
		}
		else if(aleat==3) {
			//Randomize bombs
			var bomb = this.bombs.create(1200, 340,'bomb');
			bomb.body.velocity.x = this.velocity;
			bomb.animations.add('idle', [0, 1]);
			bomb.animations.add('explode', [2, 3]);
			//bomb.events.onAnimationComplete.add( function(){bomb.kill()}, this); 
			bomb.animations.play('idle', 10,true);
		}
		else if(aleat==4) {
			//Randomize bombs
			var bomb = this.bombs.create(1200, 260,'bomb');
			bomb.body.velocity.x = this.velocity;
			bomb.scale.setTo(0.75, 0.75);
			bomb.animations.add('idle', [0, 1]);
			bomb.animations.add('explode', [2, 3]);
			//bomb.events.onAnimationComplete.add( function(){bomb.kill()}, this); 
			bomb.animations.play('idle', 10,true);
		}
		
		if(this.transicion_abajo==true){
			if(this.player.body.y<300)this.player.body.y += 2;
			if(this.scale<1)this.scale+=0.007;
			if(this.distancia<70)this.distancia+=1;
			this.player.body.x += 2;
			
			if(this.player.body.y>=300){
					this.transicion_abajo=false;
					this.carretera_abajo=true;
					this.player.body.setSize(50,75,25,25);
			}
		}
		else if(this.transicion_arriba==true){
			if(this.player.body.y>240)this.player.body.y -= 2;
			if(this.scale>0.75)this.scale-=0.007;
			if(this.distancia>55)this.distancia-=1;
			this.player.body.x -= 2;
			
			if(this.player.body.y<=240){
					this.transicion_arriba=false;
					this.carretera_abajo=false;
					this.player.body.setSize(45,70,20,20);
			}
		}
		else if(this.modo_salto==true){
			if (this.count_salto>=15){this.player.body.y-=14;}
			else if (this.count_salto>=10){this.player.body.y-=7;}
			else if (this.count_salto>=5){this.player.body.y+=7;}
			else {this.player.body.y+=14;}


			if (this.count_salto<=0) this.modo_salto=false;
			this.player.animations.play('jump',this.fps,true);
			this.shadow.animations.play('jump',this.fps,true);
			this.shadow2.animations.play('jump',this.fps,true);
			this.count_salto--;
			if (this.cursors.left.isDown){
				this.player.body.x -= 4;
			}
			else if (this.cursors.right.isDown){
				this.player.body.x += 4;
			}
		}
		else {
			if (this.cursors.left.isDown){
				this.player.body.x -= 4;
			}
			else if (this.cursors.right.isDown){
				this.player.body.x += 4;
			}
			else if (this.cursors.down.isDown & !this.carretera_abajo){
					//Change to the other lane
					this.transicion_abajo=true;
			}
			else if (this.cursors.up.isDown && this.carretera_abajo){
					this.transicion_arriba=true;
			}

			else{
				//  Stand still	
				if(this.player.body.x>150)this.player.body.x -= 2;
			}
			if (this.jumpButton.isDown&&!this.modo_salto){
				this.player.animations.play('jump',this.fps,true);
				this.shadow.animations.play('jump',this.fps,true);
				this.shadow2.animations.play('jump',this.fps,true);
				//shadow.body.velocity.y = -350;
				//shadow2.body.velocity.y = -300;
				//player.body.velocity.y = -100;
				//player.body.gravity.y = 9;
				this.modo_salto=true;
				this.count_salto=19;
				//player.body.y+=100;
			}
			if(this.pauseButton.isDown){
				if (this.game.paused){
					this.game.paused = false;
						//this.game.paused;
				}
				else{
					this.game.paused = true;
				}		
			}
		}
	},
	render:function () {

		this.game.debug.renderSpriteInfo(this.player, 32, 32);
		this.game.debug.renderSpriteCollision(this.player, 32, 400);
		this.game.debug.renderSpriteBody(this.player);

	},
	
	/* Funciones Auxilares*/
	
	collectDiamond:function (player, diamond) {

		if(!this.transicion_abajo && !this.transicion_arriba && ((this.carretera_abajo&&diamond.body.y==350)|| (!this.carretera_abajo&&diamond.body.y==270))){
			// Removes the star from the screen
			diamond.kill();
			//  Add and update the score
			this.score += 10;
			this.scoreText.content = 'Score: ' + this.score;
			
			//Aumentar velocidad
			if((this.score!=0)&&(this.score%50==0)){
				   

					this.velocity-=38;
					this.velocity_background-=1;
					this.fps+=3;
					this.diamonds.forEachAlive(function(item){item.body.velocity.x=this.velocity},this);
					this.bombs.forEachAlive(function(item){item.body.velocity.x=this.velocity},this);

					//diamonds.forEach(function(item) {
						//item.body.velocity.x = velocity;
					//}
			}
		}
	},
	touchBomb: function  (player, bomb) {
		if(!this.transicion_abajo && !this.transicion_arriba && ((this.carretera_abajo&&bomb.body.y==340)||(!this.carretera_abajo&&bomb.body.y==260))){

			bomb.animations.play('explode', 10 ,false);
			bomb.events.onAnimationComplete.add(function(bomb){bomb.kill()}, this);

			// Removes the star from the screen
			//bomb.kill();
			this.player.body.x-=40;
			//Aumentar velocidad
			if(this.health>0){this.health--;}
			else{ this.die();}
			this.healthText.content = 'Health: ' + this.health;
			if(this.score>0){
					//  Add and update the score
					this.score -= 20;
					this.scoreText.content = 'Score: ' + this.score;
					if(this.velocity<-245)this.velocity+=85;
					if(this.velocity_background<-6)this.velocity_background+=2;
					if(this.fps>20)this.fps-=2;
					this.diamonds.forEachAlive(function(item){item.body.velocity.x=this.velocity},this)
					this.bombs.forEachAlive(function(item){item.body.velocity.x=this.velocity},this);

			}
		}
	},


	killBomb: function (bomb) {
		// Removes the bomb from the screen
		bomb.kill();
	},
	die: function () {
		this.game.state.start('DeadMenu');

	}

	
};
